package com.design.springboot.booksharing.component;

import com.design.springboot.booksharing.bean.ActiveUser;
import com.design.springboot.booksharing.constant.SystemConstant;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

/**
 * 修改参数配置，所有的请求里面添加 updateBy，createBy
 *
 * @author admin
 */
@Configuration
public class ModifyParametersConfiguration {

    @Bean
    public FilterRegistrationBean modifyParametersFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new ModifyParametersFilter());
        // 拦截路径
        registration.addUrlPatterns("/*");
        // 拦截器名称
        registration.setName("modifyParametersFilter");
        // 顺序
        registration.setOrder(1);
        return registration;
    }

    /**
     * 自定义拦截器
     */
    private static class ModifyParametersFilter extends OncePerRequestFilter {
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
            ModifyHttpServletRequestWrapper requestWrapper = new ModifyHttpServletRequestWrapper(request);
            filterChain.doFilter(requestWrapper, response);
        }
    }

    private static class ModifyHttpServletRequestWrapper extends HttpServletRequestWrapper {
        private ActiveUser activeUser = null;

        ModifyHttpServletRequestWrapper(HttpServletRequest request) {
            super(request);
            HttpSession session = request.getSession();
            this.activeUser = (ActiveUser) session.getAttribute(SystemConstant.ACTIVE_USER);
        }

        @Override
        public Enumeration<String> getParameterNames() {
            if (activeUser != null) {
                Enumeration<String> enumeration = super.getParameterNames();
                ArrayList<String> list = Collections.list(enumeration);
                list.add(SystemConstant.CREATE_BY);
                list.add(SystemConstant.UPDATE_BY);
                return Collections.enumeration(list);
            }
            return super.getParameterNames();
        }

        @Override
        public String[] getParameterValues(String name) {
            if (activeUser != null) {
                if (SystemConstant.CREATE_BY.equals(name) || SystemConstant.UPDATE_BY.equals(name)) {
                    String attribute = activeUser.getAccount();
                    return new String[]{attribute};
                }
            }
            return super.getParameterValues(name);
        }
    }
}
